/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.genalungu.filesplitter.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 *
 * @author Sony
 */
public class FileWorker {


    private FileInputStream input = null;
    private FileOutputStream output = null;

    public void clean() {
        try {
            if (input != null) {
                input.close();
            }
            if (output != null) {
                output.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void splitFile(String filename, String targetDir, int limitKb, FileWorkerListener ml) {
        int outputNumber = 0;
        try {
            File inputFile = new File(filename);
            input = new FileInputStream(inputFile);
            byte[] bytes = new byte[1024];
            int read = -1;
            int counterKb = 0;

            while ((read = input.read(bytes)) != -1) {
                if (counterKb == 0) {
                    File outputFile = new File(targetDir, inputFile.getName() + "." + ++outputNumber);
                    output = new FileOutputStream(outputFile);
                }
                output.write(bytes, 0, read);
                counterKb++;
                // echoProcess(filename + "." + outputNumber, counterKb, false);
                if (ml != null) {
                    ml.echoMessage(getProcessMessage(outputNumber, counterKb, false));
                }

                if (counterKb >= limitKb) {
                    output.close();
                    counterKb = 0;
                    // echoFinished(filename + "." + outputNumber);
                    if (ml != null) {
                        ml.echoMessage("[" + outputNumber + "] Finished");
                    }
                }
            }
            output.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (input != null) {
                    input.close();
                }
                if (output != null) {
                    output.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }

    public void mergeFile(String filename, FileWorkerListener ml) {
        int outputNumber = 0;

        try {
            output = new FileOutputStream(filename);

            while (new File(filename + "." + ++outputNumber).exists()) {
                input = new FileInputStream(filename + "." + outputNumber);
                byte[] bytes = new byte[1024];
                int read = -1;
                int counterKb = 0;
                while ((read = input.read(bytes)) != -1) {
                    output.write(bytes, 0, read);
                    counterKb++;
                    // echoProcess(filename + "." + outputNumber, counterKb, true);
                    if (ml != null) {
                        ml.echoMessage(getProcessMessage(outputNumber, counterKb, true));
                    }
                }
                input.close();
                // echoFinished(filename + "." + outputNumber);
                if (ml != null) {
                    ml.echoMessage("[" + outputNumber + "] Finished");
                }
            }
            output.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (input != null) {
                    input.close();
                }
                if (output != null) {
                    output.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }

    private void echoProcess(String filename, int counterKb, boolean read) {
        System.out.print(".");
        if (counterKb % 128 == 0) {
            System.out.println("\n[" + filename + "] "
                    + (read ? "Read " : "Written ") + counterKb + " Kb");
        }
    }

    private String getProcessMessage(int fileNumber, int counterKb, boolean read) {
        if (counterKb % 128 == 0) {
            return "[" + fileNumber + "] " + (read ? "Read " : "Written ") + counterKb + " Kb";
        }
        return null;
    }

    private void echoFinished(String filename) {
        System.out.println("\n[" + filename + "] Finished!");
    }

    public static void main(String[] args) {
        String filename = "C:\\DATA\\Audio\\test.mp3";
        int limitKb = 1000;
        //new mergeFile(filename, null);
    }



}
